package mse.eai.esb;

import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.naming.Context;
import javax.naming.InitialContext;

import org.jboss.soa.esb.actions.BeanConfiguredAction;
import org.jboss.soa.esb.message.Message;


import api.ejb.ISocializer;

@Stateless(name = "CallSocializerBean")
@Local
public class CallSocializerBean implements BeanConfiguredAction{
	
	//@EJB(mappedName = "java:/PhaseBook/ISocializer/local")
	ISocializer socializer;
	
	
	public Message process(Message message) {
		
		
	
		
		
		try {
			
			Context context = new InitialContext();
			socializer = (ISocializer) context.lookup("PhaseBook/ISocializer/local");
			
			System.out.println("\n\n>>>>>>>>>>>>>>>> CALLING SOCIALIZER >>>>>>>>>>>>>>>>>>>>>\n\n");
			
		
		String action = (String) message.getBody().get("action");
		
		if(action.equals("MAKE")) {
			System.out.println("\n\n>>>>>>>>>>>>>>>> CALLING SOCIALIZER MAKE >>>>>>>>>>>>>>>>>>>>>\n\n");
			int origUserId = (Integer) message.getBody().get("origUserId");
			int destUserId = (Integer) message.getBody().get("destUserId");
			
			socializer.sendFriendshipInvitation(origUserId, destUserId);
			
		}
		if(action.equals("CONFIRM")) {
			System.out.println("\n\n>>>>>>>>>>>>>>>> CALLING SOCIALIZER CONFIRM >>>>>>>>>>>>>>>>>>>>>\n\n");

			int uid = (Integer) message.getBody().get("uid");
			int requesterId = (Integer) message.getBody().get("requesterId");
			socializer.confirmFriendshipRequest(uid, requesterId);
			
		}
		
		
		
		
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return message;
		
	}
}
